package bravo.xc.com.xc_bravo_android.push;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.beans.JpushBeans;
import bravo.xc.com.xc_bravo_android.operation.message.MessageActivity;
import bravo.xc.com.xc_bravo_android.operation.messagedetails.MessageDetailsActivity;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * CREATED BY YANK
 * DATE : 2017/11/7
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class OperationReceiver extends BroadcastReceiver {

    private Bundle bundle;
    private String result;

    @Override
    public void onReceive(Context context, Intent intent) {
        JPushInterface.setSilenceTime(context,23,59,23,59);

        bundle = intent.getExtras();

        if(intent.getAction()== JPushInterface.ACTION_NOTIFICATION_CLICK_ACTION||intent.getAction()== JPushInterface.ACTION_NOTIFICATION_OPENED){
            Log.e("@@onReceive1",bundle.toString());
            result = bundle.getString(JPushInterface.EXTRA_EXTRA);
            JpushBeans jpushBeans = new Gson().fromJson(result,JpushBeans.class);
            Log.e("@@onReceive2",result);
            Intent intent1 = new Intent(context,MessageDetailsActivity.class);
            intent1.putExtra("orderNum",jpushBeans.getOrderNum());
            context.startActivity(intent1);
        }
        if(intent.getAction() == JPushInterface.ACTION_MESSAGE_RECEIVED || intent.getAction()  == JPushInterface.ACTION_NOTIFICATION_RECEIVED || intent.getAction() == JPushInterface.ACTION_NOTIFICATION_RECEIVED_PROXY){
            Log.e("@@onReceive4",bundle.toString());
            result = bundle.getString(JPushInterface.EXTRA_EXTRA);
            JpushBeans jpushBeans = new Gson().fromJson(result,JpushBeans.class);
            Log.e("@@onReceive5",result);
                if(jpushBeans.getSound().equals("order")){
                    MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.neworder);
                    mediaPlayer.start();

                }else if(jpushBeans.getSound().equals("pay")){
                    MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.pay);
                    mediaPlayer.start();

            }
        }


    }

}
